$(".add_btn>.btn").on("click",function(){
    $(".modal_t").show()
});
$(".close_a").on("click",function(){
    $(".modal_t").hide()
})
$('.code-login').click(function(){
        // settime(this);
    var list = $("#daili_hpone").val();
    var reg =  /^1[3|4|5|7|8][0-9]{9}$/;
    if(!reg.test(list)){
        sweetAlert("",'手机号格式不正确！',"error");
        return false;
    }
    if (list == ""){
        sweetAlert("",'手机号不能为空！',"error");
        return false;
    }
    var account = $("#daili_hpone").val();
    var parmy = {"phone_num":account};
    var this_ = this;
    $.post("/admin/Login/login_code",parmy,function(respnose){
        if(respnose.success=='OK'){
            var i = 60;
            $(this_).attr('disabled',true);
            $(this_).val('重新发送('+i+')');
            var valb = window.setInterval(function() {
                i = i-1;
                $(this_).val('重新发送('+i+')');
                if(i==0){
                    window.clearInterval(valb);
                    $(this_).attr('disabled',false).val('获取验证码');
                }
            },1000);
        }else if(respnose.error>0){
            sweetAlert("",respnose.message,"error");
            setTimeout(function() {
                switch(respnose.error){
                    case '4'://已登录状态
                    window.location.href = '/admin/index';
                        break;
                }
            },3000);
        }else{
            sweetAlert("",'系统异常请联系管理员！',"error");
        }
    },'json');
    return false;
});

$('#PhoneLoginForm').ajaxForm({
	beforeSubmit: function () {
        var account = $("#daili_hpone").val();
        if (account == "") {
            sweetAlert("","手机号格式不正确！", "error");
            return false; 
        }
        var status = $('input[name="status"]').val();
        if(status==1){
            var user_yzm = $("#user_yzm").val();
            if (user_yzm == ""){
                sweetAlert("","请输入验证码！", "error");
                return false;
            }
        }else{
            var user_yzm = $('input[name="password"]').val();
            if (user_yzm == ""){
                sweetAlert("","请输入密码！", "error");
                return false;
            }
        }
    },
    success : function (respnose) {
        if(respnose.success=='OK'){
            window.localStorage.JinDouCheToken = respnose.JinDouCheToken;
            window.localStorage.UserPhone = respnose.data.phone;
            if(respnose.password==1){
                $('.modal_t').css('display','block');
                return false;
            }
            window.location.href = '/admin/index';
        }else if(respnose.error>0){
                switch(respnose.error){
                    case '7'://已登录状态
                    setTimeout(function() {
                        window.location.href = '/admin/index';
                    },3000);
                        break;
                    default:
                    sweetAlert("",respnose.message, "error");
                        break;
                }
        }else{
            sweetAlert("",'登录失败，请联系管理员！', "error");
        }
    },
    dataType: 'json',
    xhrFields: {
       withCredentials: true
    }
});
$('#ActionAddAdmin').ajaxForm({
    beforeSubmit: function () {
       var password_set = $('input[name="password_set"]').val();
       var confirm_passwd_set = $('input[name="confirm_passwd_set"]').val();
       var reg_password = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/;
       if(!reg_password.test(password_set)){
        sweetAlert("",'密码格式：6-10位，需包含字母、数字的组合。请确认！', "warning");
        return false;
       }
       if(!reg_password.test(confirm_passwd_set)||password_set!=confirm_passwd_set){
        sweetAlert("",'两次密码输入不一致。请确认！', "warning");
        return false;
       }
    },
    success : function (respnose) {
        if(respnose.success=='OK') {
            window.location.href = '/admin/index';
        } else {
            sweetAlert("",'登录失败，请联系管理员！', "error");
        }
    },
    dataType: 'json',
    xhrFields: {
       withCredentials: true
    }
});
login();
//登录
function login(){
    var token = window.localStorage.JinDouCheToken;
    var phone = window.localStorage.UserPhone;
    if(token&&phone){
        ajax({
            'url':'/admin/login/LoginComputer',
            'type':'post',
            'data':{'JinDouCheToken':token,'mobilephone':phone,'status':'3'},
            'beforeSubmit':function(){
                $('.jiazai').css('display','block');
            },
            'success':function(respnose){
                $('.jiazai').css('display','none');
                if(respnose.success=='OK'){
                    window.localStorage.JinDouCheToken = respnose.JinDouCheToken;
                    window.localStorage.UserPhone = respnose.data.phone;
                    
                    window.location.href = '/admin/index';
                }else if(respnose.error>0){
                        switch(respnose.error){
                            case '7'://已登录状态
                            setTimeout(function() {
                                 window.location.href = '/admin/index';
                            },2000);
                                break;
                            default:
                            window.localStorage.JinDouCheToken = '';
                            window.localStorage.UserPhone = '';
                            sweetAlert("",respnose.message, "error");
                                break;
                        }
                }else{
                    sweetAlert("",'登录失败，请联系管理员！', "error");
                }
            },
            'dataType':'json',
            xhrFields: {
               withCredentials: true
            }
        });
    }
}
//判断IE版本
IEVersion();
function IEVersion() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if(isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if(fIEVersion == 7) {
            $(".phone_span0").show();
            $(".phone_span1").show();
            $(".phone_span2").show();
            $("#daili_hpone").focus(function(){
                $(".phone_span0").hide();
            });
            $("#daili_hpone").blur(function(){
                if($("#daili_hpone").val().length){
                    $(".phone_span0").hide();
                }else{
                    $(".phone_span0").show();
                }
            });
            $("#user_yzm").focus(function(){
                $(".phone_span1").hide();
            });
            $("#user_yzm").blur(function(){
                if($("#user_yzm").val().length){
                    $(".phone_span1").hide();
                }else{
                    $(".phone_span1").show();
                }
            });
            $("#password_ma").focus(function(){
                $(".phone_span2").hide();
            });
            $("#password_ma").blur(function(){
                if($("#password_ma").val().length){
                    $(".phone_span2").hide();
                }else{
                    $(".phone_span2").show();
                }
            });
        } else if(fIEVersion == 8) {
            $(".phone_span0").show();
            $(".phone_span1").show();
            $(".phone_span2").show();
            $("#daili_hpone").focus(function(){
                $(".phone_span0").hide();
            });
            $("#daili_hpone").blur(function(){
                if($("#daili_hpone").val().length){
                    $(".phone_span0").hide();
                }else{
                    $(".phone_span0").show();
                }
            });
            $("#user_yzm").focus(function(){
                $(".phone_span1").hide();
            });
            $("#user_yzm").blur(function(){
                if($("#user_yzm").val().length){
                    $(".phone_span1").hide();
                }else{
                    $(".phone_span1").show();
                }
            });
            $("#password_ma").focus(function(){
                $(".phone_span2").hide();
            });
            $("#password_ma").blur(function(){
                if($("#password_ma").val().length){
                    $(".phone_span2").hide();
                }else{
                    $(".phone_span2").show();
                }
            });
        } else if(fIEVersion == 9) {
            $(".phone_span0").show();
            $(".phone_span1").show();
            $(".phone_span2").show();
            $("#daili_hpone").focus(function(){
                $(".phone_span0").hide();
            });
            $("#daili_hpone").blur(function(){
                if($("#daili_hpone").val().length){
                    $(".phone_span0").hide();
                }else{
                    $(".phone_span0").show();
                }
             });
            $("#user_yzm").focus(function(){
                $(".phone_span1").hide();
            });
            $("#user_yzm").blur(function(){
                if($("#user_yzm").val().length){
                    $(".phone_span1").hide();
                }else{
                    $(".phone_span1").show();
                }
            });
            $("#password_ma").focus(function(){
                $(".phone_span2").hide();
            });
            $("#password_ma").blur(function(){
                if($("#password_ma").val().length){
                    $(".phone_span2").hide();
                }else{
                    $(".phone_span2").show();
                }
            });
        }
    }
}
$(".password_span").on("click",function(){
   var status = $('.password_span_span').attr('status');
   if(status==1){
        $("#position_password").show();
        $("#yc_d").hide().find('input[name="checkcode"]').val('');
        $('.password_span_span').html('验证码').attr('status','2');
        $('input[name="status"]').val('2');
    }else{
        $("#position_password").hide().find('input[name="password"]').val('');
        $("#yc_d").show();
        $('.password_span_span').html('密码').attr('status','1');
        $('input[name="status"]').val('1');
    }
});